limsBatch.detailTopPage = {

    getBtnToolbar : function() {
        let _this = this;
        return {
            type : 'btnToolbar',
            items : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.BACK',
                icon : 'back',
                onClick : function() {
                    if (Gikam.isEmpty(_this.param.topPage)){
                        workspace.window.goBack();
                    }else{
                        Gikam.getLastModal().close();
                    }
                }
            }]
        }
    },

    getBaseInfoTab : function() {
        var _this = this;
        return {
            type : 'tab',
            panels : [ {
                title : 'MODULE.BUSINESS.ORDERTASK.TAB.SAMPLE_INFO',
                items : [ {
                    type : 'grid',
                    id : 'batch-sample-detail-top-list-grid',
                    url : limsBatch.batchSampleUrl + '/select/samples',
                    requestData : {
                        batchId_EQ : Gikam.isEmpty(_this.param.id) ? '-1' : _this.param.id
                    },
                    columns : limsBatch.getSampleListColumns(),
                    onCellClick : function(field, row) {
                        field === 'sampleCode' && _this.editSampleDetail(row);
                    },
                    deleteFormatter: function (row) {
                        return row.sampleCode || row.id
                    }
                } ]
            }, {
                title : 'MODULE.BUSINESS.ORDERTASK.TAB.SAMPLE_LOGISTICS_INFO',
                items : [ {
                    type : 'grid',
                    url : limsBatch.batchSampleUrl + '/select/samples',
                    requestData : {
                        batchId_EQ : Gikam.isEmpty(_this.param.id) ? '-1' : _this.param.id
                    },
                    onCellClick : function(field, row) {
                        field === 'sampleCode' && _this.editLogisticsDetail(row);
                    },
                    service : 'limsServiceImpl',
                    id : 'logistics-info-grid',
                    columns : limsBatch.getLogisticsListColumns()
                } ]
            }, {
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: 'lims-batch-special-uploader',
                    dbTable: 'T_LIMS_BATCH',
                    readonly : true,
                    bizId: Gikam.isEmpty(_this.param.id) ? '-1' : _this.param.id,
                }]
            }]
        }
    },

    editSampleDetail : function (row) {
        var _this = this;
        var model = Gikam.create('modal', {
            title: 'GIKAM.BUTTON.EDIT',
            width : 600,
            height : 450,
        });
        Gikam.create('layout', {
            renderTo: model.window.$dom,
            center: {
                items: [{
                    type: 'form',
                    id: 'batch-sample-code-form',
                    columns : 2,
                    titleWidth : 110,
                    readonly : true,
                    fields: limsBatch.getSampleFormFields(),
                    autoSave: false,
                    onRendered: function () {
                        this.hideFields(['sampleCode']);
                        row.productNames = row.ext$.productnames;
                        this.setData(row);
                    }
                }]
            }
        })
    },

    editLogisticsDetail: function (row) {
        var _this = this;
        Gikam.createLimsModal({
            title: 'GIKAM.BUTTON.EDIT',
            width : 600,
            height : 300,
            hideButton: true,
            onConfirm: function () {
                var form = Gikam.getComp('sample-logistics-form');
                if (form.validate()) {
                    var data = form.getData();
                    var chooseData = {
                        id : row.id,
                        temperatureLimits : data.temperatureLimits,
                        senderName : data.senderName,
                        senderPhone : data.senderPhone,
                        courierNumber : data.courierNumber,
                        courier : data.courier,
                        senderRemark : data.senderRemark,
                    }
                    Gikam.put(limsBatch.sampleUrl + '/instant', Gikam.getJsonWrapper(null, [null, [chooseData]])).done(function () {
                        Gikam.getComp('logistics-info-grid').refreshRowById(row.id)
                    })
                    Gikam.getLastModal().close(data);
                }
            },
            m: {
                type: 'form',
                id: 'sample-logistics-form',
                columns : 2,
                titleWidth : 100,
                readonly : true,
                fields: limsBatch.getSampleLogisticsFormFields(),
                onRendered: function () {
                    this.setData(row)
                }
            },
        })
    },

    createPage : function() {
        Gikam.create('layout', {
            center : {
                items : [ this.getBtnToolbar(), this.getBaseInfoTab() ]
            },
            toolbarAlign: 'right',
            renderTo : Gikam.isEmpty(this.param.topPage) ? workspace.window.$dom : Gikam.getLastModal().window.$dom
        });
        return this;
    },

    init : function(param) {
        this.param = param;
        this.createPage();
    }
}
